/*
 * 文件名：LoginFacade.java
 * 版权：MoonBox 
 * 修改人：Peter
 * 修改时间：2018年10月25日
 * 修改内容：新增
 */
package com.renzhebiao.lkang.facade;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.renzhebiao.lkang.dto.LoginDTO;
import com.renzhebiao.lkang.entity.UserPO;
import com.renzhebiao.lkang.po.LoginVO;
import com.renzhebiao.lkang.service.ILoginService;
import com.renzhebiao.lkang.service.IUserService;
import com.renzhebiao.lkang.token.JwtTokenUtil;
import com.renzhebiao.lkang.utils.EmptyTools;
import com.renzhebiao.lkang.utils.MapUtils;

import io.jsonwebtoken.Claims;
import lombok.extern.log4j.Log4j;

/**
 * <p>
 * 登陆接口
 * 
 * @author Peter
 * @since 1.0.0
 */
@Log4j
@Service
public class LoginFacade {

    @Autowired
    private JwtTokenUtil  jwtUtil;
    @Autowired
    private IUserService  userService;

    @Autowired
    private ILoginService loginService;

    public LoginVO login(LoginDTO login) {
        UserPO user = loginService.loginAdmin(login);
        String token = this.jwtUtil.generateToken(new MapUtils().put("id", user.getId()));
        return LoginVO.builder().token(token).userName(user.getUserName()).id(user.getId()).build();

    }

    public String refresh(String token) {
        Claims claims = this.jwtUtil.getClaimsFromToken(token);
        Long id = null;
        if (EmptyTools.isNotNullOrEmpty(claims)) {
            id = (Long) claims.get("id");
        }
        if (EmptyTools.isNullOrEmpty(id)) {
            log.error("token失败,未能取得用户信息.");
            return "";
        }
        return this.jwtUtil.refreshToken(token);
    }

}
